package com.shensi.adminCollect.controller.fleet;


import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.shensi.common.core.controller.BaseController;
import com.shensi.common.core.domain.AjaxResult;
import com.shensi.common.core.page.PageDomain;
import com.shensi.common.core.page.TableSupport;
import com.shensi.adminCollect.common.utils.PageInfoUtil;
import com.shensi.adminCollect.domain.fleet.FleetCardSub;
import com.shensi.adminCollect.service.fleet.FleetCardSubService;
import com.shensi.adminCollect.domain.station.PayOrder;
import com.shensi.adminCollect.service.station.IPayOrderService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/fleet/payOrder")
@Slf4j
public class FleetCardPayOrderController extends BaseController {

    @Autowired
    private FleetCardSubService fleetCardSubService;

    @Autowired
    private IPayOrderService payOrderService;


    /**
     * 车队卡消费列表  车队卡id 唯一
     * */
    @GetMapping("/list")
    @ApiOperation(value = "车队卡消费列表 ", notes = "车队卡消费列表 ")
    public AjaxResult list(PayOrder payOrder) {

        if(Objects.isNull(payOrder.getPayType()) || !"cdk".equals(payOrder.getPayType()) || Objects.isNull(payOrder.getConsumerId())){
            return AjaxResult.error("请求参数异常");
        }
        log.info("FleetCardPayOrderController.list请求参数:[{}]",JSONObject.toJSONString(payOrder));
        List<PayOrder> payOrderAllList = new ArrayList<>();
        // 主卡以及子卡消费记录都要查询
        if("fleet_card".equals(payOrder.getCdkType())) {
            payOrder.setCdkId(payOrder.getConsumerId());
            payOrder.setConsumerId(null);
            // 查询主卡消费记录
            List<PayOrder> maxPayOrderList = payOrderService.selectNewCdkPayOrderList(payOrder);

            List<FleetCardSub> fleetCardSubs = fleetCardSubService.selectAllByFleetCardId(payOrder.getCdkId());
            log.info("fleetCardSubs:[{}]",JSONObject.toJSONString(fleetCardSubs));
            List<Long> cdkIdList = fleetCardSubs.stream().map(FleetCardSub::getId).collect(Collectors.toList());
            List<PayOrder> minPayOrderList = new ArrayList<>();
            if(!CollectionUtils.isEmpty(cdkIdList)) {
                payOrder.setCdkIdList(cdkIdList);
                payOrder.setCdkId(null);
                payOrder.setCdkType("fleet_card_sub");
                // 查询子卡消费记录
                minPayOrderList = payOrderService.selectNewCdkPayOrderList(payOrder);
            }
            maxPayOrderList.addAll(minPayOrderList);
            List<PayOrder> payOrderList = maxPayOrderList.stream().sorted(Comparator.comparing(PayOrder::getCreatedDate)).collect(Collectors.toList());
            // 按时间从大到小排序
            Collections.reverse(payOrderList);
            payOrderAllList.addAll(payOrderList);

        } else if("fleet_card_sub".equals(payOrder.getCdkType())) {
            // 只需要查询子卡消费记录
            payOrder.setCdkId(payOrder.getConsumerId());
            payOrder.setConsumerId(null);
            List<PayOrder> payOrders = payOrderService.selectNewCdkPayOrderList(payOrder);
            log.info("payOrders:[{}]",JSONObject.toJSONString(payOrders));
            List<PayOrder> payOrderList = payOrders.stream().sorted(Comparator.comparing(PayOrder::getCreatedDate)).collect(Collectors.toList());
            // 按时间从大到小排序
            Collections.reverse(payOrderList);
            payOrderAllList.addAll(payOrderList);
        }

        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        log.info("pageNum:[{}],pageSize:[{}],payOrderAllList:[{}]",pageNum,pageSize, JSONObject.toJSONString(payOrderAllList));
        PageInfo pageInfo = PageInfoUtil.listPageInfo(pageNum, pageSize, payOrderAllList);

        return AjaxResult.success(pageInfo);
    }

}
